package demo;

import ru.nsu.fit.iconveyor.conveyor.node.InnerNode;
import ru.nsu.fit.iconveyor.conveyor.node.NodeConnection;

import java.util.*;

/**
 * @author Timur Zolotuhin (tzolotuhin@gmail.com)
 */
public class FileNameDelimiterNode extends InnerNode {
    public static final String INPUT_ARG = "input_arg";
    public static final String INPUT_ARG_LOOP = "input_arg_loop";
    public static final String OUTPUT_ARG_LOOP = "output_arg_loop";
    public static final String OUTPUT_RESULT = "output_result";

    private final NodeConnection<ArrayList> inputArg;
    private final NodeConnection<ArrayList> inputArgLoop;
    private final NodeConnection<ArrayList> outputArgLoop;
    private final NodeConnection<String> outputResult;

    public FileNameDelimiterNode(String id) {
        super(id);

        inputArg = new NodeConnection<ArrayList>(ArrayList.class, NodeConnection.Type.INPUT, INPUT_ARG);
        inputArgLoop = new NodeConnection<ArrayList>(ArrayList.class, NodeConnection.Type.INPUT, INPUT_ARG_LOOP);
        outputArgLoop = new NodeConnection<ArrayList>(ArrayList.class, NodeConnection.Type.OUTPUT, OUTPUT_ARG_LOOP);
        outputResult = new NodeConnection<String>(String.class, NodeConnection.Type.OUTPUT, OUTPUT_RESULT);

        addNodeConnection(inputArg);
        addNodeConnection(inputArgLoop);
        addNodeConnection(outputArgLoop);
        addNodeConnection(outputResult);
    }

    @Override
    public Map<String, Object> execute(Map<String, Object> inputBundles, Properties prop) {
        Map<String, Object> result = new HashMap<String, Object>();

        if (inputBundles.get(INPUT_ARG) != null) {
            Object inputArgBundle = inputBundles.get(INPUT_ARG);
            if (inputArgBundle != null && inputArgBundle.getClass() != null && inputArgBundle.getClass() == ArrayList.class) {
                ArrayList<String> coll = (ArrayList<String>)inputArgBundle;
                if (coll.size() > 0) {
                    Iterator<String> iterator = coll.iterator();
                    Object obj = iterator.next();
                    result.put(OUTPUT_RESULT, obj);
                    iterator.remove();
                }
                if (coll.size() > 0) {
                    result.put(OUTPUT_ARG_LOOP, coll);
                }
            }
        } else if (inputBundles.get(INPUT_ARG_LOOP) != null){
            Object inputArgLoopBundle = inputBundles.get(INPUT_ARG_LOOP);
            if (inputArgLoopBundle != null && inputArgLoopBundle.getClass() != null && inputArgLoopBundle.getClass() == ArrayList.class) {
                ArrayList<String> coll = (ArrayList<String>)inputArgLoopBundle;
                if (coll.size() > 0) {
                    Iterator<String> iterator = coll.iterator();
                    Object obj = iterator.next();
                    result.put(OUTPUT_RESULT, obj);
                    iterator.remove();
                }
                if (coll.size() > 0) {
                    result.put(OUTPUT_ARG_LOOP, coll);
                }
            }
        }

        return result;
    }

    @Override
    public Map<String, Object> preExecute(Map<String, Object> availableInputBundles, Properties prop) {
       if (availableInputBundles.get(INPUT_ARG) != null) {
           availableInputBundles.put(INPUT_ARG_LOOP, null);
       } else if (availableInputBundles.get(INPUT_ARG_LOOP) != null){
           availableInputBundles.put(INPUT_ARG, null);
       } else {
           return null;
       }
        return availableInputBundles;
    }

    @Override
    public int getOutputToInputRatio() {
        //TODO: check
        return 2;
    }
}
